<?php

/**
 * @name ViewAdapter
 * @auther zpp
 * @date 2014-08-18
 */
class ViewAdapter extends Yaf_View_Simple {

    protected $autoSwitchH5 = false;

    public $layout = 'default';
    public $pageCss = '';
    public $pageJs = '';
    public $pageTitle = '';
    public $pageController = '';
    public $pageAction = '';
    public $User;
    public $menus = [];
    public $Jsplugins = [];
    public $Cssplugins = [];
    public $shareConfig = [];
    public $pageNav = 'default';
    public $params = [];
    public $pageFooter = 'zls';
    public $curUser;

    public function autoSwitchH5($set = true) {
        $this->autoSwitchH5 = $set;
    }

    public function display($tplname, $tplvars = []) {
        echo $this->render($tplname, $tplvars);
    }

    /**
     * @param       $tplname
     * @param array $tplvars
     */
    public function render($tplname, $tplvars = []) {
        $tplname = $this->_fixTplPath($tplname);
        if ($this->autoSwitchH5 && Util::isMobile()) {
            $pathinfo = pathinfo($tplname);
            $h5_tplname = $pathinfo['dirname'] . '/' . $pathinfo['filename'] . '.mobi.' . $pathinfo['extension'];
            if (file_exists($this->_tpl_dir . '/' . $h5_tplname)) {
                $tplname = $h5_tplname;
            }
        }
        $content = parent::render($tplname, $tplvars);
        if ($this->layout) {
            $content = parent::render($this->_fixLayoutPath($this->layout), ['content' => $content]);
        }
        return $content;
    }

    public function getBaseUri() {
        return Yaf_Dispatcher::getInstance()->getRequest()->getBaseUri();
    }

    protected function _fixLayoutPath($layoutname) {
        if (strpos($layoutname, '.') === false) {
            $layoutname .= '.phtml';
        }
        $pos = strpos($layoutname, '/');
        if ($pos === false) {
            $layoutname = 'layout/' . $layoutname;
        } elseif ($pos === 0) {
            $layoutname = '../../views/layout/' . $layoutname;
        }
        return $layoutname;
    }

    protected function _fixTplPath($tplname) {
        if (strpos($tplname, '.') === false) {
            $tplname .= '.phtml';
        }
        $pos = strpos($tplname, '/');
        if ($pos === false) {
            $tplname = strtolower(Yaf_Dispatcher::getInstance()->getRequest()->getControllerName()) . '/' . $tplname;
        } elseif ($pos === 0) {
//            $this->setScriptPath(APPLICATION_PATH . '/views');
//            $tplname = substr($tplname, 1);
            $tplname = '../../views' . $tplname;
        }
        return $tplname;
    }

    public function renderPartial($tplname, $tplvars = [], $return = false) {
        $tplname = $this->_fixTplPath($tplname);
        if ($return) {
            return parent::render($tplname, $tplvars);
        }
        echo parent::render($tplname, $tplvars);
    }

    public function renderPartialJSON($tplname, $tplvars = [], $params = []) {
        $html = $this->renderPartial($tplname, $tplvars, true);
        $this->renderAjax(['html' => $html, 'params' => $params]);
    }

    public function renderAjax($obj = null, $errMsg = null, $errCode = 0, $params = []) {
        if (isset($errMsg)) {
            $json = array(
                'ok' => 0,
                'msg' => $errMsg,
                'code' => $errCode
            );
        } else {
            $json = array(
                'ok' => 1,
                'res' => isset($obj) ? $obj : new stdClass(),
            );
        }
        $json = array_merge($json, $params);
        Yaf_Dispatcher::getInstance()->disableView();
        $jsonStr = json_encode($json, JSON_UNESCAPED_UNICODE);
        header('Content-Type: application/json; charset=utf-8');
        echo $jsonStr;
        return true;
    }

}
